<?php
/**
 * User: Lijun
 * Date: 2017/7/4.
 * Time: 15:46.
 * Comment:
 */

namespace Admin\Controller;
use Think\Controller;

class ClassInfoController extends Controller{

    public function index(){
        $category = M("category") -> where(array("pid" => 0)) -> order("sort asc") -> select();
        foreach($category as $k=> $v ) {
            $category[$k]['child'] = M("category") -> where(array("pid" => $v['id'])) -> select();
        }
        $this -> assign("category",$category);
        $this -> display();
    }

    public function addClass(){
        $category = M("category") -> where(array("pid" => 0)) -> order("sort asc") -> select();
        foreach($category as $k=> $v ) {
            $category[$k]['child'] = M("category") -> where(array("pid" => $v['id'])) -> select();
            }
        $this -> assign("category",$category);
        $this -> display();
    }

    public function doAddClass(){
        $info["pid"] = I("post.pid");
        $pid = M("category") -> where(array("id" => $info["pid"])) -> getField("arrparentid");
        if(!empty($pid)){
            $info["arrparentid"] = $pid.",".$info["pid"];
        }else{
            $info["arrparentid"] = $info["pid"];
        }
        $catname = I("post.catname");
        preg_match_all("/./u",$catname,$arr_name);
        foreach($arr_name[0] as $v){
            $py .= getFirstChar($v);
        }
        $info["catname"] = I("post.catname");
        $info["pname"] = strtolower($py);
        $info["content"] = $_POST['content'];
        if($info['pid'] == 6){
            $info["url"] = I("post.url");
        }elseif($info["pid"] == 0){
            $info["url"] = "/index.php/".$info["pname"].'.html';
        }else{
            $pname = M("category") -> where(array("id" => $info["pid"])) -> getField("pname");
            $info["url"] = "/index.php/".$pname.'/'.$info["pname"];
        }
        if($_FILES['image']['error'] != 4){
            $upload = new \Think\Upload();
            $upload -> maxSize = 3145728;
            $upload -> exts = array("jpg","jpeg","png","gif");
            $upload -> autoSub = false;
            $upload -> rootPath = "./Public/";
            $upload -> savePath = "uploads/";
            if($image = $upload -> upload()){
                $info["image"] = "/Public/".$image["image"]["savepath"].$image["image"]["savename"];
            }else{
                $this -> error("error");
            }
        }
        $info["time"] = time();
        $info["sort"] = I("post.sort");
        if(M("category") -> add($info)){
            $this -> success("添加成功",U("index"));
        }else{
            $this -> error("添加失败");
        }
    }

    public function modClass(){
        $id = I("id");
        $info = M('category') -> where("id = {$id}") -> select();
        $category = M("category") -> where(array("pid" => 0)) -> order("sort asc") -> select();
        foreach($category as $k=> $v ) {
            $category[$k]['child'] = M("category") -> where(array("pid" => $v['id'])) -> select();
        }
        $this -> assign("category",$category);
        $this -> assign('info',$info[0]);
        $this -> display();
    }

    public function doModSubClass(){
        $id = I("post.id");
        $pid = I("post.pid");
        if($id != $pid){
            $arrparentid = M("category") -> where(array("id" => $pid)) -> getField("arrparentid");
            if($arrparentid == 0){
                $info["pid"] = $pid;
                $info["arrparentid"] = $arrparentid.",".$pid;
            }
        }
        $catname = I("post.catname");
        preg_match_all("/./u",$catname,$arr_name);
        foreach($arr_name[0] as $v){
            $py .= getFirstChar($v);
        }
        $info["catname"] = I("post.catname");
        $info["pname"] = strtolower($py);
        $info["content"] = $_POST['content'];
        $apid = M("category") -> where(array("id" => $id)) -> getField("pid");
//        if($apid == 0){
//            $info["url"] = "/index.php/".$info["pname"];
//        }else{
//            $pname = M("category") -> where(array("id" => $apid)) -> getField("pname");
//            $info["url"] = "/index.php/".$pname.'/'.$info["pname"].'.html';
//        }
        if($_FILES['image']['error'] != 4){
            $upload = new \Think\Upload();
            $upload -> maxSize = 3145728;
            $upload -> exts = array("jpg","jpeg","png","gif");
            $upload -> autoSub = false;
            $upload -> rootPath = "./Public/";
            $upload -> savePath = "uploads/";
            if($image = $upload -> upload()){
                $info["image"] = "/Public/".$image["image"]["savepath"].$image["image"]["savename"];
            }else{
                $this -> error("error");
            }
        }
        $info["time"] = time();
        $info["sort"] = I("post.sort");
        if($sql = M("category") -> where(array("id" => $id)) -> save($info)){
            $this -> success("修改成功",U("index"));
        }else{
            $this -> error("修改失败");
        }
    }

    public function delClass(){
        if(IS_GET){
            $id = I('id');
            if(M('category') -> where("id = {$id}") -> delete()){
                $this -> success('删除成功',U('index'));
            }else{
                $this -> error('删除失败');
            }
        }

        if(IS_POST){
            $ids = $_POST['id'];
            $id = implode(',', $ids);
            if(M('category') -> where("id in ({$id})") -> delete()){
                $this -> success('删除成功',U('index'));
            }else{
                $this -> error('删除失败');
            }
        }
    }





}